package chapt07.imaging;

import java.awt.event.*;
import javax.media.opengl.GLProfile;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ImagingSwingGLCanvas implements ActionListener
{
	private static GLCanvas glcanvas;
	
 	static
	{
		// setting this true causes window events not to get sent on Linux if
		// you run from inside Eclipse
		GLProfile.initSingleton(false);
	}
	
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
 	private static void createAndShowGUI()
 	{
		GLProfile glprofile = GLProfile.getDefault();
		GLCapabilities glcapabilities = new GLCapabilities(glprofile);
		glcanvas = new GLCanvas(glcapabilities);
		
		glcanvas.addGLEventListener(new ImagingGLEventListener());
		
		final JFrame jframe = new JFrame("OpenGL Imaging subset");
		jframe.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent windowevent)
			{
				jframe.dispose();
				System.exit(0);
			}
		});
		
        //Create/set menu bar and content pane.
        ImagingSwingGLCanvas demo = new ImagingSwingGLCanvas();
 
        //Create and set up the popup menu.
        demo.createPopupMenu(glcanvas);
		
		jframe.getContentPane().add(glcanvas, BorderLayout.CENTER);
		jframe.setSize(525, 550);
		jframe.setVisible(true);
 	}
 	
 	public void createPopupMenu(GLCanvas glcanvas)
 	{
        JMenuItem menuItem;
        
        //Create the popup menu.
        JPopupMenu popup = new JPopupMenu();
        menuItem = new JMenuItem("Save Image");
        menuItem.addActionListener(this);
        popup.add(menuItem);
        menuItem = new JMenuItem("Raw Stretched Image");
        menuItem.addActionListener(this);
        popup.add(menuItem);
        menuItem = new JMenuItem("Increase Contrast");
        menuItem.addActionListener(this);
        popup.add(menuItem);
        menuItem = new JMenuItem("Invert Color");
        menuItem.addActionListener(this);
        popup.add(menuItem);
        menuItem = new JMenuItem("Emboss Image");
        menuItem.addActionListener(this);
        popup.add(menuItem);
        menuItem = new JMenuItem("Sharpen Image");
        menuItem.addActionListener(this);
        popup.add(menuItem);
        menuItem = new JMenuItem("Histogram");
        menuItem.addActionListener(this);
        popup.add(menuItem);
 
        //Add listener to the text area so the popup menu can come up.
        MouseListener popupListener = new ImagingPopupListener(popup);
        glcanvas.addMouseListener(popupListener);
 	}
 	
	public static void main(String[] args)
	{
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
	}
	
    public void actionPerformed(ActionEvent e)
    {
        JMenuItem source = (JMenuItem)(e.getSource());
        String text = source.getText();
        if(text == "Save Image")
        	Imaging.processMenuValue = 0;
        else if(text == "Raw Stretched Image")
        	Imaging.processMenuValue = 1;
        else if(text == "Increase Contrast")
        	Imaging.processMenuValue = 2;
        else if(text == "Invert Color")
        	Imaging.processMenuValue = 3;
        else if(text == "Emboss Image")
        	Imaging.processMenuValue = 4;
        else if(text == "Sharpen Image")
        	Imaging.processMenuValue = 5;
        else if(text == "Histogram")
        	Imaging.processMenuValue = 6;
        
        // Refresh the Window
		glcanvas.display();
    }
}